package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Admin;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author liu
 * @since 2022-01-27
 */
@Component
public interface AdminMapper extends BaseMapper<Admin>{


    @Update("UPDATE `t_admin` set name=#{name},telephone=#{telephone},phone=#{phone},remark=#{remark}" +
            ",address=#{address} WHERE id=#{id};\n")
    void updUserInfo(@Param("name") String name, @Param("telephone") String telephone, @Param("phone") String phone,
                     @Param("remark") String remark, @Param("address") String address, @Param("id") Integer id);


    @Update("UPDATE `t_admin` set user_face=#{user_face} WHERE id=#{id};\n")
    void updUserFace(@Param("user_face") String user_face, @Param("id") Integer id);

    @Select("select count(username) from `t_admin` where username=#{username}")
    int queryUserName(@Param("username") String username);

    @Select("select id from `t_admin` where username=#{username}")
    int queryIdByUserName(@Param("username") String username);

    @Insert("INSERT into `t_admin` (username,password,name,phone,telephone,address,remark,enabled,user_face)\n" +
            "VALUES(#{username},#{password},#{name},#{phone},#{telephone},#{address},#{remark},#{enabled},#{user_face})")
    void addUser(@Param("username") String username,@Param("password") String password,@Param("name") String name,
                 @Param("phone") String phone,@Param("telephone") String telephone,@Param("address") String address,
                 @Param("remark") String remark,@Param("enabled") String enabled,@Param("user_face") String user_face);

    @Insert("INSERT into t_admin_role (admin_id,rid) values(#{admin_id},#{rid})")
    void addUserRole(@Param("admin_id")int admin_id,@Param("rid")int rid);

    @Select("SELECT admin.*\n" +
            "FROM `t_admin` as admin,`t_admin_role` as adminrole\n" +
            "WHERE admin.id = adminrole.admin_id AND adminrole.rid=(SELECT id from t_role WHERE name='ROLE_person')")
    List<Admin> queryAllUser();

    @Update("UPDATE `t_admin` set enabled=#{enabled} WHERE id=#{id}")
    void enabledUser(@Param("id") int userid,@Param("enabled") String enabled);

    @Delete("DELETE t_admin,t_admin_role FROM t_admin,t_admin_role\n" +
            "WHERE t_admin.id=t_admin_role.admin_id AND t_admin.id=#{userid}")
    void delUserById(@Param("userid") int userid);

}
